<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>1.正则</title>
</head>

<body>
  <input type="text" id="name"><span id="username"></span><br>
  <button>确认</button>
</body>
<script>
  var button = document.getElementsByTagName('button')[0]
  var username = document.getElementById('username')
  //用构造函数创建
  /* 
    i忽略大小写
    g全局匹配
    gi效果叠加
    m多行匹配
  */
  var reg = new RegExp(/admin/i)
  //直接量创建
  var reg1 = /admin/i
  //reg1.test()判断传入的值是否符合正则，返回Boolean
  button.onclick = function () {
    var name = document.getElementById('name')
    console.log(name.value);
    if (reg1.test(name.value)) {
      console.log(11111)
      username.innerHTML = '账号正确'
    } else {
      console.log(222222222)
      username.innerHTML = '账号bu正确'
    }
  }
  var str = 'abcdefg'
  var reg2 = /ab/i
  console.log('reg2=' + reg2.test(str))
  console.log('reg2=' + reg2.exec(str))
  console.log(reg2.exec(str))
  //lastIndex:下一次匹配开始的索引位置，可以修改和读取
  console.log(reg2.lastIndex)
  reg2.lastIndex = 5
  console.log(reg2.lastIndex)
  var str1 = 'daw asfgg gaegeaff'
  var reg3 = /gg/gi
  reg3.lastIndex = 8
  console.log(reg3.exec(str1));
</script>

</html>